package com.clms.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.common.JwtUtils;
import com.clms.system.mapper.UserMapper;
import com.clms.system.myconfig.RsaKeyProperties;
import com.clms.system.pojo.entitry.SysUser;
import com.clms.system.pojo.vo.LoginVo;
import com.clms.system.pojo.vo.UserVo;
import com.clms.system.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @program: clms-third
 * @description:用户的控制类
 * @author: 大鱼
 * @create: 2021-07-16 14:55
 **/
@Service
public class UserSreviceimpl implements UserService {
        @Autowired
        UserMapper userMapper;
    @Autowired
    RsaKeyProperties properties;
    @Override
    public R Findbynameandpassword(LoginVo loginVo) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("user_name", loginVo.getUsername());
//        queryWrapper.eq("status", 0);
//        SysUser user = userMapper.selectOne(queryWrapper);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken= new UsernamePasswordToken(loginVo.getUsername(), loginVo.getPassword());
        try {
            subject.login(usernamePasswordToken);
            SysUser user = new SysUser();
            user = (SysUser) subject.getPrincipal();
            user.setPassword("");
            String token= JwtUtils.generateTokenExpireInMinutes(user,properties.getPrivateKey(),2*60);
            return R.ok().put("token", token);

        } catch (Exception e) {e.printStackTrace();
            return R.error();
        }

    }


    @Override
    public R findAllUserPage(Integer currentPage, Integer pageSize, UserVo userVo) {
        Page<SysUser> page = new Page<>(currentPage, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty (userVo.getEmail()))
        {
            queryWrapper.like("email",userVo.getEmail());
        }
        if(StringUtils.isNotEmpty(userVo.getLoginName()))
        {
            queryWrapper.like("login_name",userVo.getLoginName());
        }

        if(StringUtils.isNotEmpty(userVo.getPhone()))
        {
            queryWrapper.like("phonenumber",userVo.getPhone());
        }
        if(StringUtils.isNotEmpty(userVo.getUserName()))
        {
            queryWrapper.like("user_name", userVo.getUserName());
        }
        Page page1 = userMapper.selectPage(page, queryWrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    @Override
    public R updataUserStatus(Integer userId, Integer status) {
        userMapper.updataUserStatus(userId, status);
        return R.ok().message("修改成功");
    }

    @Override
    public R deleteUserById(Integer roleId) {
        userMapper.deleteById(roleId);
        return R.ok().message("删除成功");
    }

    @Override
    public R userAddOrUpdate(SysUser userinfo) {
        System.out.println(userinfo.getUserId());
        if (userinfo.getUserId()!=0) {
            userMapper.updateById(userinfo);
            return R.ok().message("修改成功");
        } else {
            userMapper.insert(userinfo);
            return R.ok().message("插入成功");
        }

    }

    @Override
    public R getUserById(Integer userId) {
        SysUser user = new SysUser();
        user = userMapper.selectById(userId);
        return R.ok().put("userinfo",user);
    }

    @Override
    public R Checkbyname(Map map) {
        QueryWrapper queryWrappe = new QueryWrapper();
        queryWrappe.eq("user_name", map.get("same"));
        SysUser user = userMapper.selectOne(queryWrappe);
        if (user == null) {
            return R.ok().put("type",true);
        } else {
            if (map.get("id").equals(String.valueOf(user.getUserId()))) {
                return R.ok().put("type",true);
            }
            return R.error().put("type",false);
        }

    }

    @Override
    public R resetById(String userId) {
        SysUser user = userMapper.selectById(userId);
        Md5Hash md5Hash = new Md5Hash(123456,user.getSalt(),1024);
        System.out.println(md5Hash.toString());
        user.setPassword(md5Hash.toString());
        userMapper.insert(user);
        return R.ok().message("重置成功");
    }


}
